﻿package com.CrmDemo.services;

import java.io.Serializable;

import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;

import com.CrmDemo.util.*;

import com.CrmDemo.dao.CommonDAO;
import com.CrmDemo.entity.CstCustomer;


/**
 * 用户表 管理业务逻辑类
 * 
 * @author hailong.liu
 */
public class CustomerService extends BaseService {
	/* 复杂业务方法 */

	/* 简单业务方法 */
	/**
	 * 加载 用户表
	 */
	
	
	public CstCustomer get(Serializable id) {
		CstCustomer ret = (CstCustomer) this.getCommonDAO().get(
				CstCustomer.class, id);
		return ret;
	}

	/**
	 * 添加 用户表
	 */
	public boolean add(CstCustomer item) {
		this.getCommonDAO().add(item);
		return true;
	}

	/**
	 * 删除 用户表
	 */
	public boolean del(Serializable id) {
		CstCustomer item = this.get(id);
		item.setCustStatus("3");
		this.getCommonDAO().update(item);
		return true;
	}

	/**
	 * 修改 用户表
	 */
	public boolean update(CstCustomer item) {
		this.getCommonDAO().update(item);
		return true;
	}

	/**
	 * 查询 用户表
	 */
	public void search(CstCustomer condition, PageResult pageResult) {
		String hql = "select o from CstCustomer o where 1=1 ";
		if (null != condition) {
			if (isNotNullOrEmpty(condition.getCustName())) {
				hql += "and o.custName like '%" + condition.getCustName()
						+ "%' ";
			}

			if (isNotNullOrEmpty(condition.getCustRegion())) {
				hql += "and o.custRegion like '%" + condition.getCustRegion()
						+ "%' ";
			}

			if (isNotNullOrEmpty(condition.getCustManagerName())) {
				hql += "and o.custManagerName like '%"
						+ condition.getCustManagerName() + "%' ";
			}

			if (isNotNullOrEmpty(condition.getCustLevelLabel())) {
				hql += "and o.custLevelLabel like '%"
						+ condition.getCustLevelLabel() + "%' ";
			}

			if (isNotNullOrEmpty(condition.getCustAddr())) {
				hql += "and o.custAddr like '%" + condition.getCustAddr()
						+ "%' ";
			}

			if (isNotNullOrEmpty(condition.getCustZip())) {
				hql += "and o.custZip like '%" + condition.getCustZip() + "%' ";
			}

			if (isNotNullOrEmpty(condition.getCustTel())) {
				hql += "and o.custTel like '%" + condition.getCustTel() + "%' ";
			}

			if (isNotNullOrEmpty(condition.getCustFax())) {
				hql += "and o.custFax like '%" + condition.getCustFax() + "%' ";
			}

			if (isNotNullOrEmpty(condition.getCustWebsite())) {
				hql += "and o.custWebsite like '%" + condition.getCustWebsite()
						+ "%' ";
			}

			if (isNotNullOrEmpty(condition.getCustLicenceNo())) {
				hql += "and o.custLicenceNo like '%"
						+ condition.getCustLicenceNo() + "%' ";
			}

			if (isNotNullOrEmpty(condition.getCustChieftain())) {
				hql += "and o.custChieftain like '%"
						+ condition.getCustChieftain() + "%' ";
			}

			if (isNotNullOrEmpty(condition.getCustBank())) {
				hql += "and o.custBank like '%" + condition.getCustBank()
						+ "%' ";
			}

			if (isNotNullOrEmpty(condition.getCustBankAccount())) {
				hql += "and o.custBankAccount like '%"
						+ condition.getCustBankAccount() + "%' ";
			}

			if (isNotNullOrEmpty(condition.getCustLocalTaxNo())) {
				hql += "and o.custLocalTaxNo like '%"
						+ condition.getCustLocalTaxNo() + "%' ";
			}

			if (isNotNullOrEmpty(condition.getCustNationalTaxNo())) {
				hql += "and o.custNationalTaxNo like '%"
						+ condition.getCustNationalTaxNo() + "%' ";
			}

			if (isNotNullOrEmpty(condition.getCustStatus())) {
				hql += "and o.custStatus like '%" + condition.getCustStatus()
						+ "%' ";
			}

		}
		if (isNotNullOrEmpty(pageResult.getOrderBy())) {
			String sort = pageResult.getSort();
			hql += "order by " + pageResult.getOrderBy() + " " + sort;
			if ("asc".equals(sort)) {
				pageResult.setSort("desc");
			} else {
				pageResult.setSort("asc");
			}
		} else {
			hql += "order by o.custNo desc";
		}
		this.getCommonDAO().listByPage(hql, pageResult);
	}

	public CstCustomer getWithLinkmans(String id) {
		CstCustomer ret = this.get(id);
		Hibernate.initialize(ret.getCstLinkmans());
		return ret;
	}

	public CstCustomer getWithActivities(String id) {
		CstCustomer ret = this.get(id);
		Hibernate.initialize(ret.getCstActivities());
		return ret;
	}
}
